﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Reflection;

namespace WPFInterview
{
    /// <summary>
    /// Interaction logic for QuestionsPanel.xaml
    /// </summary>
    public partial class QuestionsPanel : Window
    {
        List<IQuestion> _questionsList = new List<IQuestion>();

        public QuestionsPanel()
        {
            InitializeComponent();

            // Initial the list Box
            InitListBox();
        }

        private void InitListBox()
        {
            // Get a list of interface
            var types = from p in Assembly.GetExecutingAssembly().GetTypes()
                        where (p.GetInterface("IQuestion") != null)
                        select p;

            // Get a list
            _questionsList = (from p in types
                           orderby p.Name
                           select Activator.CreateInstance(p) as IQuestion).ToList();
            _questionsList.Sort((a, b) => a.TestName.CompareTo(b.TestName));

            _questionsListBox.ItemsSource = _questionsList;
            _questionsListBox.DisplayMemberPath = "TestName";

            // Fill out the welcome panel
            _questionPanel.Children.Clear();
            _questionPanel.Children.Add(new WelcomeTest().getUserControl());
        }

        private void _questionsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // Check whether any item is selected.
            if (e.AddedItems.Count > 0)
            {
                // Clean up the client area
                _questionPanel.Children.Clear();

                // Add child
                _questionPanel.Children.Add(((IQuestion)e.AddedItems[0]).getUserControl());
            }
        }

        // Executed event handler.
        private void CloseCommandHandler(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("Close Command");
        }

        // CanExecute event handler.
        private void CanExecuteHandler(object sender, CanExecuteRoutedEventArgs e)
        {
            // e.CanExecute
            e.CanExecute = true;
        }

        private void AddNew_Executed(object sender, ExecutedRoutedEventArgs e)
        {

        }

        private void AddNew_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {

        }
    }
}
